<template>
	<view class="pages-yxlm">
		<!-- #ifdef MP-WEIXIN -->
		<loading v-if="0 != loading" :status="loading" />
		<web-view :src="yxlmUrl" @message="receiveMessage"></web-view>
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		baseDomain,
		baseUrl,
		baseApiUrl,
		wechatAuthUrl,
		callBackUrl
	} from '../../utils/constant.js'
	import {
		http
	} from '@/utils/commonRequest.js'; // 局部引入
	let util = require('../../utils/util');

	export default {
		data() {
			return {
				title: 'Hello，进入页面会默认调用checkLogin接口',
				yxlmUrl: null, // 营销联盟
				loading: 1 // 1加载中,0加载成功,-1加载失败
			}
		},
		onLoad(options) {
			// #ifdef H5
			let params = {
				callBackUrl: callBackUrl
			}
			this.checkLogin(params)
			// #endif
			// #ifdef MP-WEIXIN
			let that = this;
			let jumpLink = options.jumpLink;

			if (!util.isEmpty(jumpLink)) {
				uni.setStorageSync('QYPT_YXLM_JUMP_LINK', jumpLink);
			}

			http
				.get('/yxlmOpenId/getYxlmUrl', null)
				.then((res) => {
					res = res.data

					if (200 == res.code) {
						let link = res.yxlmUrl
						if (link.indexOf('&amp;') > -1) {
						  link = link.replaceAll('&amp;', '&')
						}
						that.yxlmUrl = link
					} else {
						uni.hideLoading();
						uni.navigateBack();
					}
				})
				.catch((err) => {
					console.log(err);
					uni.hideLoading();
					uni.navigateBack();
				});
			// #endif
		},
		methods: {
			// #ifdef H5
			checkLogin: function(params) {
				uni.showLoading({
					title: "加载中"
				})
				http
					.get('/qmh/getYxlmUrl', {
						params: params
					})
					.then(res => {
						uni.hideLoading()
						if (200 == res.data.code) {
							location.replace(res.data.yxlmUrl)
						}
					})
					.catch(err => {
						uni.hideLoading()
						uni.switchTab({
							url: "/pages/index/index"
						})
						console.log('局部test 获取用户列表 get fail----');
						console.log(err);
					});
			},
			// #endif
			// #ifdef MP-WEIXIN
			receiveMessage: function(e) {
				let data = e.detail.data[0];

				if (!util.isEmpty(data)) {
					uni.setStorageSync('QYPT_QMH_OPENID_DATA', data);
				}

			 this.saveQmhOpenId(data);
			},

			saveQmhOpenId(data) {
				let jumpLink = uni.getStorageSync('QYPT_YXLM_JUMP_LINK');

			 if (util.isEmpty(jumpLink)) {
					return;
				}

				if (!util.isEmpty(data)) {
					let params = {
						data: data
					};
					util.showLoading('加载中');
					http
			  	.post('/gzzhMiniappC/saveQmhOpenid', params)
						.then((res) => {
							res = res.data
							console.log('saveQmhOpenid接口返回res', res);

							if (200 == res.code) {
								let memberInfo = res.memberInfo;
								app.globalData.memberInfo = memberInfo;
								uni.removeStorageSync('QYPT_YXLM_JUMP_LINK');

								if (!util.isEmpty(jumpLink)) {
									uni.redirectTo({
										url: decodeURIComponent(jumpLink)
									});
								}
							}

							util.hideLoading();
						})
						.catch((err) => {
							util.hideLoading();
							console.log(err);
						});
				}
			}
			// #endif
		}
	}
</script>

<style>
</style>
